package com.ruoyi.web.controller.template;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ChatBoxInfo;
import com.ruoyi.system.domain.ChatLog;
import com.ruoyi.system.domain.ChatMessages;
import com.ruoyi.system.service.iChatLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@RestController
@RequestMapping("/chat/log")
public class ChatLogController extends BaseController {

    @Autowired
    private iChatLogService chatLogService;

    @PreAuthorize("@ss.hasPermi('chat:log:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(ChatLog chatLog) {
        startPage();
        List<ChatLog> list = chatLogService.selectChatLogList(chatLog);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('chat:log:list')")
    @GetMapping("/unReply")
    @Anonymous
    public TableDataInfo selectChatByMaxTime(ChatLog chatLog) {
        startPage();
        List<ChatLog> list = chatLogService.selectChatByMaxTime(chatLog);
        return getDataTable(list);
    }


    /**
     * 历史记录
     */
    @PreAuthorize("@ss.hasPermi('chat:ui:list')")
    @GetMapping("/{customerNumber}/{clientNumber}")
    public TableDataInfo createClient(@PathVariable("customerNumber") String customerNumber,@PathVariable("clientNumber") String clientNumber) {
        ChatLog chatLog = new ChatLog();
        chatLog.setClientNumber(clientNumber);
        chatLog.setCustomerNumber(customerNumber);
        List<ChatLog> list = chatLogService.selectChatLogList(chatLog);
        return getDataTable(list);
    }

    @Log(title = "会话数据导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('chat:log:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatLog user) {
        List<ChatLog> list = chatLogService.selectChatLogList(user);
        ExcelUtil<ChatLog> util = new ExcelUtil<ChatLog>(ChatLog.class);
        util.exportExcel(response, list, "会话数据");
    }

    @Log(title = "会话数据导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('chat:log:export')")
    @PostMapping("/timeExport")
    public void timeExport(HttpServletResponse response, ChatLog chatLog) {
        List<ChatLog> list = chatLogService.selectChatByMaxTime(chatLog);
        ExcelUtil<ChatLog> util = new ExcelUtil<ChatLog>(ChatLog.class);
        util.exportExcel(response, list, "会话数据");
    }


    @PreAuthorize("@ss.hasPermi('customer:user:chat')")
    @GetMapping("/messages/{fansId}")
    public AjaxResult getMessagesByFansId(@PathVariable("fansId") String fansId) {
        if (StringUtils.isNull(fansId)) {
            return error("粉丝不能为空");
        }
        List<ChatMessages> chatboxList = chatLogService.getMessagesByFansId(fansId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", chatboxList);
        return ajax;
    }
}